//-------------------------------------------------------------------------
// File: EnvironmentBlock.cpp
//
// Descr.: - todo -
//
// Author: Stefan Leihkauf
// Date:   28.07.2013
//-------------------------------------------------------------------------
#include "cb_pch.h"
#include "EnvironmentBlock.h"

namespace cb { namespace process {

//-------------------------------------------------------------------------
// EnvironmentBlock
//-------------------------------------------------------------------------

const StdString EnvironmentBlock::terminationString = _T("");

//-------------------------------------------------------------------------
/**
*/
void EnvironmentBlock::add(const EnvironmentString& e)
{
	for(iterator iter = this->begin(); iter != this->end(); ++iter) {
		if(iter->getName() == e.getName()) { // if key exists -> just value updates
			iter->setValue(e.getValue());
			return;
		}
	}
	this->_environment.push_back(e);
}

//-------------------------------------------------------------------------
/**
*/
void EnvironmentBlock::remove(const EnvironmentString& e)
{
	for(iterator iter = this->begin(); iter != this->end(); ++iter) {
		if((*iter) == e) {
			_environment.erase(iter);
			return;
		}
	}
}

//-------------------------------------------------------------------------
/**
*/
const EnvironmentString& EnvironmentBlock::find(const StdString& name) const
{
	for(unsigned i = 0; i < _environment.size(); i++) {
		if(_environment[i].getName() == name)
			return _environment[i];
	}
	return EnvironmentString::Empty;
}


//-------------------------------------------------------------------------
/**
*/
EnvironmentBlock::StringList EnvironmentBlock::toString() const
{
	EnvironmentBlock::StringList strList;
	for(const_iterator iter = this->begin(); iter != this->end(); ++iter) {
		strList.push_back(iter->toString());
	}

	strList.push_back(EnvironmentBlock::terminationString);
	return strList;
}

//-------------------------------------------------------------------------
/**
*/
bool EnvironmentBlock::operator == (const EnvironmentBlock& other) const
{
	if(other.size() != this->size())
		return false;

	const_iterator iterThis = this->begin();
	const_iterator iterOther = other.begin();

	while(iterThis != this->end()) {
		if((*iterThis) != (*iterOther))
			return false;

		++iterThis;
		++iterOther;
	}

	return true;
}

}} // namespace cb::process